package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;

    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;


    /**
     * 新增订单的业务方法
     *
     * @param orders
     * @param userId
     */
    @Override
    @Transactional
    public void addOrder(Orders orders, Long userId) {
        //order已经封装了:remark,addressBookId,payMethod
        //1.根据传递过来的默认收货地址id获取地址对象AddressBook
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());

        //2.根据传递过来的登录用户id获取购物车数据List<ShoppingCart>
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAllByUserId(userId);

        //3.根据登录用户的id获取登录用户对象(得到用户名别名写入到订单数据)
        User user = userMapper.findById(userId);

        //4.完善订单对象Orders数据
        Long oid = new Long(UUIDUtils.getUUIDInOrderId());
        orders.setId(oid);
        orders.setNumber(oid + "");
        orders.setStatus(1); //1待付款
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now()); //下单时间
        orders.setCheckoutTime(LocalDateTime.now()); //结账时间

        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName()); //登录用户的别名
        orders.setConsignee(addressBook.getConsignee());//收件人

        //5.完善订单明细对象List<Order
        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();
        //定义总金额
        BigDecimal total = new BigDecimal("0");

        //遍历购物车集合数据,封装到每个OrderDetail中
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //将每个购物车的商品数据shoppingCart封装到OrderDetail对象中
            OrderDetail orderDetail = new OrderDetail();
            Long orderDetailId = new Long(UUIDUtils.getUUIDInOrderId());
            orderDetail.setId(orderDetailId);
            orderDetail.setName(shoppingCart.getName());//菜品名字或套餐名字
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());

            //将封装好的orderDetail对象加入到orderDetailList
            orderDetailList.add(orderDetail);

            //累加每个商品的金额=数量*单价
            //multiply()这个是decimal类型的乘法方法
            //add() 这个是decimal类型的加法方法
         total= total.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()+"")));
        }

        //注意:完善订单主表的总价
        orders.setAmount(total);

        //6.调用mapper新增订单主表数据
        orderMapper.save(orders);

        //7.调用mapper新增多条订单明细表数据
        orderDetailMapper.saveBatch(orderDetailList);

        //8.下单成功后清空购物车
        shoppingCartMapper.deleteByUserId(userId);

    }
}
